package com.example.localizadorv4;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
	private BluetoothAdapter mBluetoothAdapter;
	MyReceiver myReceiver;
	private static final int REQUEST_ENABLE_BT = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button arrancar = (Button) findViewById(R.id.Activar);
		arrancar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				arrancarNuevoServicio();
			}
		});
		Button parar = (Button) findViewById(R.id.Parar);
		parar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				pararNuevoServicio();
			}
		});		
	}
	protected void onStart() {
		 // TODO Auto-generated method stub
		 
		      //Register BroadcastReceiver
		      //to receive event from our service
		      myReceiver = new MyReceiver();
		      IntentFilter intentFilter = new IntentFilter();
		      intentFilter.addAction(Servicio.MY_ACTION);
		      registerReceiver(myReceiver, intentFilter);
		     
		  
		 
		 super.onStart();
		}
	// Arrancar el servicio
		public void arrancarNuevoServicio() {
			
		      // Use this check to determine whether BLE is supported on the device.  Then you can
	        // selectively disable BLE-related features.
	        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
	            Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
	            finish();
	        }

	        // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
	        // BluetoothAdapter through BluetoothManager.
	        final BluetoothManager bluetoothManager =
	                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
	        mBluetoothAdapter = bluetoothManager.getAdapter();

	        // Checks if Bluetooth is supported on the device.
	        if (mBluetoothAdapter == null) {
	            Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
	            finish();
	            return;
	        }
			Intent i= new Intent(getBaseContext(), Servicio.class);
			this.startService(i);
		}

		// Parar el servicio
		public void pararNuevoServicio() {

			stopService(new Intent(this, Servicio.class));

		}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			Intent intent = new Intent(this, Settings.class);
		    startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	protected void onStop() {
	 // TODO Auto-generated method stub
	 unregisterReceiver(myReceiver);
	 super.onStop();
	}
	
	private class MyReceiver extends BroadcastReceiver{
		 
		 @Override
		 public void onReceive(Context arg0, Intent arg1) {
		  // TODO Auto-generated method stub
		  
		  Double X= arg1.getDoubleExtra("DATAPASSEDX", 0);
		  Double Y= arg1.getDoubleExtra("DATAPASSEDY", 0);
		  TextView cordX = (TextView)findViewById(R.id.X);
		  TextView cordY = (TextView)findViewById(R.id.Y);
		  cordX.setText("X:  "+Double.toString(X));
	      cordY.setText("Y:  "+Double.toString(Y));
//		  Toast.makeText(MainActivity.this,
//		    "Triggered by Service!\n"
//		    + "Data passed: " + String.valueOf(datapassed),
//		    Toast.LENGTH_LONG).show();
//		  
		 }
		 
		}
}
